<?php
namespace Admin\Controller;
use Admin\Controller\AdminController;
use Think\Page;

class MessageController extends AdminController {
	public function _initialize(){
        parent::_initialize();
    }
	/**
	*��Ϣ�б�
	*/
	public function index(){
           
		//��ѯ��������
		$school_type_post = I('school_type');
		$school_post = I('school');
		$grade_post = I('grade');
		$class_post = I('class');
		$realname_post = I('realname');
		//var_dump($realname_post);exit();
		$order = I('order')?I('order'):'yang_message.add_time desc';
		if(strpos($order,'+')){
			$order = str_replace('+',' ',$order);
		}
		$post = array('school_type'=>$school_type_post,'school'=>$school_post,'grade'=>$grade_post,'class'=>$class_post,'realname'=>$realname_post,'order'=>$order);
		
		if(!empty($school_type_post)){
			$where['yang_member.school_type'] = $school_type_post;
		}
		if(!empty($school_post)){
			$where['yang_member.school'] = $school_post;
		}
		if(!empty($grade_post)){
			$where['yang_member.grade'] = $grade_post;
		}
		if(!empty($class_post)){
			$where['yang_member.class'] = $class_post;
		}
		if(!empty($realname_post)){
			$where['yang_member.realname'] = array('like',"%{$realname_post}%");
		}
				
		//�����ʾ�Ѿ��Ǽ���Ϣ�Ļ�Ա������
		$where['yang_member.status'] = 1;
		//��ҳ����
		
		$count = M("Message")
				->join("left join yang_member on yang_member.openid = yang_message.openid")
				->where($where)
				->count();
		$Page  = new \Think\Page($count,20);
		setPageParameter($Page, $post);
		$show = $Page->show();
		$list = M('Message')
				->Field('yang_member.*,yang_message.type as type,yang_message.content as content,yang_message.add_time as add_time')
				->join("left join yang_member on yang_member.openid = yang_message.openid")
				->where($where)
				->order($order)
				->limit($Page->firstRow.','.$Page->listRows)
				->select();
		
		//��ȡ���������꼶����
		$school = M('Type')->Field('id,parent_id,name')->select();
		foreach($school as $k=>$v){
			if($v['parent_id'] == 0){
				$school_type[] = $v;
			}
			if(!empty($school_type_post) && $v['parent_id'] == $school_type_post){
				
				$school_name[] = $v;
			}
			if(!empty($school_post) && $v['parent_id'] == $school_post){
				$grade[] = $v;
			}
			if(!empty($grade_post) && $v['parent_id'] == $grade_post){
				$class[] = $v;
			}
			$school[$v['id']] = $v['name'];
		}
		
		//��ʽ��ѧ���İ༶��Ϣ
		foreach($list as $k=>$v){
			$list[$k]['school_type'] = $school[$v['school_type']];
			$list[$k]['school'] = $school[$v['school']];
			$list[$k]['grade'] = $school[$v['grade']];
			$list[$k]['class'] = $school[$v['class']];
		}
		//��ѯ��ǰ�����·�����������
		$list_x = M("Message")
				->Field('yang_message.openid')
				->join("left join yang_member on yang_member.openid = yang_message.openid")
				->where($where)
				->group('yang_message.openid')
				->select();
		$count_x = count($list_x);
		//��ֵ��ҳ����
		$this->assign('count_x',$count_x);
		$this->assign('post',$post);
		$this->assign('school_type',$school_type);
		$this->assign('school_name',$school_name);
		$this->assign('grade',$grade);
		$this->assign('class',$class);
		$this->assign('list',$list);
		$this->assign('page',$show);
		$this->display();
	}
}